import Http from './http';
const app = getApp();
/**
 * @description api请求处理
 */
export default class Api {
  constructor() {
    this.http = new Http();
  }
  afterLogin(isLogin) {
    setTimeout(() => {
      wx.hideLoading();
      //返回上一层
      if (app.globalData.pathId != 0) {
        let path = app.globalData.pathId == 1 ? '/pages/dongtai/dongtai' : '/pages/user/user';
        wx.reLaunch({
          url: path,
        });
      } else {
        wx.reLaunch({
          url: '/pages/index/index',
        });
      }
    }, 1000);
  }
  /**
   * 用户注册
   * @param {JSON} data 
   */
  userRegister(data) {
    wx.showLoading({
      title: '注册中',
    });
    this.http.request('post', 'register', data, false)
      .then(res => {
        if (res.success == 2) {
          wx.showToast({
            title: '注册成功'
          });
          wx.setStorageSync('userId', res.user_id);
          wx.setStorageSync('userName', res.user_name);
          this.afterLogin(false);
        } else if (res.error == 3) {
          wx.showToast({
            title: '该账户已存在',
            icon: 'none'
          });
        }
      })
      .catch(rej => {
        console.log(rej);
        wx.showToast({
          title: '网络异常',
          icon: 'none'
        });
      });
  }
  /**
   * 用户登录
   * @param {} data 
   */
  userLogin(data) {
    wx.showLoading({
      title: '登录中',
    });
    this.http.request('post', 'login', data, false)
      .then(res => {
        if (res.success == 0) {
          wx.setStorageSync('userId', res.user_id);
          wx.setStorageSync('userName', res.user_name);
          this.afterLogin(true);
        } else {
          wx.showToast({
            title: '账号或密码错误',
            icon: 'none'
          });
        }
      })
      .catch(rej => {
        console.log(rej);
        wx.showToast({
          title: '网络异常',
          icon: 'none'
        });
      });
  }
  //获取全部科目
  async getSubject(){
    let result = null;
    await this.http.request('get', 'getsubject', {}, true)
      .then(res => {
        if(res.error){
          wx.showToast({
            title: '获取失败',
            icon: 'none'
          });
        }else {
          result = res;
        }
      })
      .catch(rej => {
        console.log(rej);
      });
    return result;
  }
  //获取章节数组
  async getChapter(data) {
    let result = null;
    await this.http.request('post', 'getchapterfromsub', data, true)
      .then(res => {
        result = res;
      })
      .catch(rej => {
        console.log(rej);
      });
    return result;
  }
  //获取题目数组
  async getTitleById(data) {
    let result = null;
    await this.http.request('post', 'gettitlefromchp', data, true)
      .then(res => {
        result = res;
      })
      .catch(rej => {
        console.log(rej);
      });
    return result;
  }
  //获取题目详情
  async getTitleDetail(data) {
    let result = null;
    await this.http.request('post', 'gettitleinfo', data, true)
      .then(res => {
        result = res;
      })
      .catch(rej => {
        console.log(rej);
      });
    return result;
  }
  //随机题目
  async getRandomTitle() {
    let result = null;
    await this.http.request('post', 'getrandomtitleinfo', {}, true)
      .then(res => {
        result = res;
      })
      .catch(rej => {
        console.log(rej);
      });
    return result;
  }
  //提交答案
  async setAnswer(data) {
    let result = null;
    await this.http.request('post', 'submitanswer', data, true)
      .then(res => {
        result = res;
      })
      .catch(rej => {
        console.log(rej);
      });
    return result;
  }
  //签到
  async setSignIn() {

  }
  //修改密码
  modyfyPsd(data) {
    this.http.request('post', 'modification', data, true)
      .then(res => {
        if(res.user_id){
          wx.showToast({
            title: '修改成功',
          });
          wx.navigateBack();
        }else {
          wx.showToast({
            title: '修改失败',
            icon: 'none'
          });
        }
      })
      .catch(rej => {
        console.log(rej);
      });
  }
}